package ar.edu.utn.frsf.proyectofinal.sistemadecontrolmodbus.presentation.controller.admin;

import java.util.Date;
import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.context.Flash;

import ar.edu.utn.frsf.proyectofinal.sistemadecontrolmodbus.persistence.model.Device;
import ar.edu.utn.frsf.proyectofinal.sistemadecontrolmodbus.presentation.controller.admin.devices.DeviceBean;
import ar.edu.utn.frsf.proyectofinal.sistemadecontrolmodbus.service.SystemService;

@ManagedBean(name="deviceAdminListBean")
@ViewScoped
public class DeviceAdminList {

	@ManagedProperty(value="#{systemService}")
	private SystemService sysServ;
	
	public SystemService getSysServ() {
		return sysServ;
	}

	public void setSysServ(SystemService sysServ) {
		this.sysServ = sysServ;
	}

	public List<DeviceBean> getDevices(){
		List<Device> devices = sysServ.getSysDevices();
		return DeviceBean.wrap(devices,sysServ);
	}
	
	public void newDevice(){
		Device newDev = new Device();
		newDev.setAutoActualization(false);
		newDev.setAutoConection(false);
		newDev.setDescription("XXX");
		newDev.setIP("localhost");
		newDev.setLastActivity(new Date());
		newDev.setName("XXX");
		newDev.setPort(502);
		newDev.setSounding(true);
		newDev.setSoundingTime(2500);
		newDev.setType(null);
		newDev.setUnitID(1);
		newDev.setVisible(true);

		Flash flash = FacesContext.getCurrentInstance().getExternalContext().getFlash();
		flash.put("selected", new DeviceBean(newDev,sysServ));
	}
}
